package com.kwin;

import redis.clients.jedis.Jedis;

public class TestSet {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.flushDB();
        System.out.println("=================向集合中添加元素================");
        System.out.println(jedis.sadd("Set", "A", "B", "C"));
        System.out.println(jedis.sadd("Set","D","E"));
        System.out.println(jedis.sadd("Set","F"));
        System.out.println("Set:" + jedis.smembers("Set"));
        System.out.println("删除一个元素A:" + jedis.srem("Set", "A"));
        System.out.println("Set的所有元素为:" + jedis.smembers("Set"));
        System.out.println("删除两个元素B C:" + jedis.srem("Set", "B", "C"));
        System.out.println("Set的所有元素为:" + jedis.smembers("Set"));
        System.out.println("随机移除集合中的一个元素:" + jedis.spop("Set"));
        System.out.println("随机移除集合中的一个元素:" + jedis.spop("Set"));
        System.out.println("Set的所有元素为:" + jedis.smembers("Set"));
        System.out.println("Set中包含元素的个数:" + jedis.scard("Set"));
        System.out.println("F是否在Set中:" + jedis.sismember("Set","F"));
        System.out.println("D是否在Set中:" + jedis.sismember("Set","D"));
        System.out.println("E是否在Set中:" + jedis.sismember("Set","E"));

        System.out.println("=================================================");
        System.out.println(jedis.sadd("Set1", "1","2","3","4","5"));
        System.out.println(jedis.sadd("Set2", "4","5","6","7","8"));
        System.out.println("Set1中删除3并移入Set2:" + jedis.smove("Set1", "Set2","3"));
        System.out.println("Set1中的元素:" + jedis.smembers("Set1"));
        System.out.println("Set2中的元素:" + jedis.smembers("Set2"));
        System.out.println("Set2中删除3并移入Set1:" + jedis.smove("Set2", "Set1","3"));
        System.out.println("Set1中的元素:" + jedis.smembers("Set1"));
        System.out.println("Set2中的元素:" + jedis.smembers("Set2"));

        System.out.println("===================集合运算===================");
        System.out.println("Set1和Set2的交集:" + jedis.sinter("Set1","Set2"));
        System.out.println("Set1和Set2的并集:" + jedis.sunion("Set1","Set2"));
        System.out.println("Set1和Set2的差集:" + jedis.sdiff("Set1","Set2"));
        jedis.sinterstore("Set3","Set1","Set2"); // 求交集并保存到一个新的key
        System.out.println("Set3中的元素:" + jedis.smembers("Set3"));
    }
}
